package helper
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	
	public class LoaderHelper extends Loader
	{
		private var _urlRequest:URLRequest;
		private var _path:String;
		private var _callBack:Function;
		private var _repeat:int;
		private var _currepeat:int=0;
		private var _data:*;//仅用来传输(过渡)其它数据
		
		public function LoaderHelper(path:String, repeat:int=0, callBack:Function=null, data:*=null)
		{
			super();
			_path=path;
			_callBack=callBack;
			_repeat=repeat;
			_data=data;
			
			_urlRequest=new URLRequest();
			_urlRequest.url=_path;
			
			setEvent();
		}
		
		private function setEvent():void
		{
			this.contentLoaderInfo.addEventListener(Event.COMPLETE, handler);
			this.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, handler);
			this.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, handler);
		}
		
		/**
		 * 加载指定的路径的请求文件 
		 * @param path 路径
		 * @param repeat 失败重试次数
		 * @param callBack 返回方法
		 */
		public function loadSync(context:LoaderContext=null):void
		{
			this.load(_urlRequest, context);
		}
		
		/**
		 *事件状态 
		 */		
		private function handler(evt:Event):void
		{  
			switch(evt.type)
			{
				case Event.COMPLETE:  
					loadCompleted(evt);
					break;  
				case IOErrorEvent.IO_ERROR:
					loadRepeat();
					break;
				case SecurityErrorEvent.SECURITY_ERROR:
					loadRepeat();
					break;
			}  
		} 
		
		/**
		 *加载成功
		 */		
		private function loadCompleted(evt:Event):void
		{
			if(_callBack!=null) _callBack(this);
		}
		
		/**
		 *重试加载
		 */		
		private function loadRepeat():void
		{
			
		}
		
		public function dispose():void
		{
			this.removeEventListener(Event.COMPLETE, handler);
			this.removeEventListener(IOErrorEvent.IO_ERROR, handler);
			this.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, handler);
			
			_urlRequest=null;
			_callBack=null;
			_data=null;
		}

		public function get data():*
		{
			return _data;
		}

		public function set data(value:*):void
		{
			_data = value;
		}

	}
}